package com.lenovo.framework.view;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.lenovo.framework.FApplication;
import com.lenovo.framework.R;
import com.lenovo.framework.biz.ContextManager;
import com.lenovo.framework.util.DLog;
import com.squareup.leakcanary.RefWatcher;
import com.trello.rxlifecycle.components.support.RxDialogFragment;

/**
 * Created by lenovo on 2016/3/31.
 */
public class DialogLoadingFragment extends RxDialogFragment {

    public static final java.lang.String GRAVITY = "gravity";
    public static final java.lang.String LAYOUT = "layout";
    public static final java.lang.String LAYOUT_HEIGHT = "layout_height";
    public static final java.lang.String LAYOUT_WIDTH = "layout_width";
    public static final String WINDOW_ANIM = "window_anim";
    public static final String DISMISSABLE = "dismiss_able";
    public static final String IS_LOGIN = "is_login";
    private Rect outRect;
    /**
     * listener to retrieve data from this fragment
     */
    protected DialogFragmentDismissListener mInterface;
    private int se;
    private Runnable runable;

    private boolean tint;

    public static DialogLoadingFragment newInstance(Bundle bundle, Runnable setup){
        DialogLoadingFragment fragment = new DialogLoadingFragment();
        fragment.setArguments(bundle);
        fragment.setRunable(setup);
        return fragment;
    }

    public DialogLoadingFragment(){
        if(getArguments() == null)
            setArguments(new Bundle());
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        //setStyle(DialogFragment.STYLE_NORMAL, R.style.BaseDialogFragmentStyle);
        //setStyle(DialogFragment.STYLE_NO_FRAME, R.style.DeepColorDialogFragment);
        //setStyle(DialogFragment.STYLE_NO_FRAME, R.style.TintColorDialogFragment);
        boolean isLogin = getArguments().getBoolean(IS_LOGIN, false);
        if(isLogin){
            setStyle(DialogFragment.STYLE_NO_FRAME, R.style.DeepColorDialogFragment);
        }else{
            setStyle(DialogFragment.STYLE_NO_FRAME, R.style.TintColorDialogFragment);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume(){
        outRect = new Rect();
        getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        // Auto size the dialog based on it's contents
        int width = getArguments().getInt(LAYOUT_WIDTH, 0);
        int height = getArguments().getInt(LAYOUT_HEIGHT, 0);
        int resId = getArguments().getInt(WINDOW_ANIM, -1);

        if(-1 != resId){
            getDialog().getWindow().setWindowAnimations(resId);
        }
        if(width != 0 && height != 0) {
            getDialog().getWindow().setLayout(width, height);
        }else{
            getDialog().getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        int layout = getArguments().getInt(LAYOUT, -1);
        int gravity = getArguments().getInt(GRAVITY, -1);
        if(gravity != Gravity.BOTTOM && gravity != -1) {
            getDialog().getWindow().setGravity(gravity);
        }
        if(layout != -1){
            getDialog().setContentView(layout);
        }
        if(getArguments().getBoolean(DISMISSABLE)) {
            setCancelable(true);
        }else{
            setCancelable(false);
        }
        if(runable != null)
            runable.run();
        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public int getLayoutHeight(View contentRelative) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        contentRelative.measure(w, h);
        int height = contentRelative.getMeasuredHeight();
        return outRect.height() - height;
    }

    public boolean isOutDistrict(int y,View view){
        int height = getLayoutHeight(view);
        return y<=height;
    }

    public void setRunable(Runnable runable) {
        this.runable = runable;
    }

    /**
     * this listener is used to retrieve dialog fragment data to the observer.
     * @author JeffreyHe
     * @version 1.0
     * @updated 26-一月-2016 14:51:51
     */
    public static interface DialogFragmentDismissListener {
        //retrieve data from the dialog fragment
        void onRetrieveDialogFragmentData(Bundle b, int Tag);
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(activity instanceof  DialogFragmentDismissListener)
            mInterface = (DialogFragmentDismissListener) activity;
        else mInterface = null;
    }

    @Override
    public void onDetach(){
        mInterface = null;
        super.onDetach();
    }


    public void onDestroy() {
        mInterface = null;
        ((FApplication) ContextManager.appContext()).forceUnregisterComponentCallbacks();
        super.onDestroy();
        DLog.d(getClass().getName(), "onDestory!");

        RefWatcher refWatcher = FApplication.getRefWatcher();
        refWatcher.watch(this);
    }
}
